<?php

namespace app\admin\controller;

use app\admin\BaseController;

class ActivityController extends BaseController
{
    /**
     * 获取商品列表
     * @return \think\response\Json
     */
    public function listsAction()
    {
        try{
            $post = $this->get;

            $params['where'][] = ['appletId', '=', $this->appletId];

            $params['order'] = ['activityId'=>'desc'];

            if(!empty($post['activityName'])){
                $params['where'][] = ['activityName','like','%'.$post['activityName'].'%'];
            }

            if(!empty($post['categoryId'])){
                $params['where'][] = ['categoryId', '=', $post['categoryId']];
            }

            $res = $this->app->activityService->getLists($params, $this->page, $this->pagesize);

            $data['lists'] = [];
            foreach($res as $key=>$vo){
                $data['lists'][] = [
                    'activityId' => $vo->activityId,
                    'categoryId' => $vo->categoryId,
                    'activityName' => $vo->activityName,
                    'activityIntro' => $vo->activityIntro,
                    'image' => $vo->image,
                    'clickCount' => $vo->clickCount,
                    'isSignup' => $vo->isSignup,
                    'status' => $vo->status,
                    'statusText' => $vo->getStatusText()
                ];
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($res);

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 详情
     * @param  [type] $id
     *
     * @return void
     */
    public function detailAction($id)
    {
        try{
            $activity = $this->app->activityService->getDetail($id);
            if(!$activity){
                throw new \Exception("活动不存在",500);
            }
            $data = $activity->toArray();

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * 添加商品
     * @return \think\response\Json
     */
    public function addAction()
    {
        try{
            if(empty($this->post)){
                throw new \Exception("没有数据提交，不能处理",500);
            }
            $this->post['adminId'] = $this->admin->adminId;
            $this->post['appletId'] = $this->appletId;
            $this->app->activityService->add($this->post);

            return json(['code' => 200, 'msg' => '添加成功', 'data'=> []]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 名录修改
     *
     * @return void
     */
    public function updateAction($id)
    {
        try {
            $activity = $this->app->activityService->getDetail($id);

            $this->app->activityService->update($activity, $this->post);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function enableAction($id)
    {
        try {
            $activity = $this->app->activityService->getDetail($id);

            $this->app->activityService->enable($activity);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function disableAction($id)
    {
        try {
            $activity = $this->app->activityService->getDetail($id);

            $this->app->activityService->disable($activity);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
}